package eightDataType;

import org.apache.commons.collections4.map.HashedMap;
import utils.PrintUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ajie
 * @date 2019-09-01
 * @function
 */
public class ByteDemo {
    public static void main(String[] args) {

        cacheFeaturePath();
        System.out.println("Byte.MAX_VALUE:"+Byte.MAX_VALUE);
        System.out.println("Byte.MIN_VALUE:"+Byte.MIN_VALUE);
        byte [] src = {1,2,3,4,5,6};

        byte[] des = new byte[2];
        System.arraycopy(src,0,des,0,2);
        PrintUtil.print(des);

        byte [] desTwo = new byte[2];
        System.arraycopy(src,2,desTwo,0,2);
        PrintUtil.print(desTwo);

        byte[] bytes = new byte[99];
        System.out.println(((byte)98));
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) (i%10);
        }
        clipByte(bytes);

        byte byTemp = 1;
        int inTemp = 129;
        byTemp = (byte) inTemp;//todo int 转 byte 需要强转。
        System.out.println("int "+inTemp+"--> byte:"+byTemp);
        inTemp = byTemp;//byte 转 int 不需要强转。
    }

    private static void cacheFeaturePath() {
        Map<String,String> cacheMap = new HashedMap<>();
        byte [] src = {1,2,3,4,5,6};
        byte [] src2 = {1,2,3,4,5,6};

        cacheMap.put(new String(src),"a.jpeg");
        System.out.println(cacheMap.get(new String(src2)));

        byte[] tempBytes = new byte[200];
        System.arraycopy(src,0,tempBytes,0,tempBytes.length>src.length?src.length:tempBytes.length);
        byte[] tempBytes2 = new byte[200];
        System.arraycopy(src2,0,tempBytes2,0,tempBytes2.length>src2.length?src2.length:tempBytes2.length);

        String temp1 = new String(tempBytes);
        String temp2 = new String(tempBytes2);
        System.out.println("tempBytes :"+ temp1);
        System.out.println("tempBytes2:"+ temp2);
        System.out.println("equals:"+ temp1.equals(temp2));
    }

    private static void clipByte(byte[] bytes) {
        int count = 0;
        int total = bytes.length;
        int byteSize = 10;
        while (count< total){
            List<byte[]> tempList = new ArrayList<>();

            if ((total - count) > byteSize){
                byte[] one = new byte[byteSize];
                System.arraycopy(bytes,count,one,0,byteSize);
                tempList.add(one);
                count +=byteSize;
            }else {
                byte[] lost = new byte[total - count];
                System.arraycopy(bytes,count,lost,0,total-count);
                tempList.add(lost);
                count +=total - count;
            }
            PrintUtil.print(tempList.get(0));
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }


}
